package com.newu.lh.databindingrecyclerview;

import android.Manifest;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.newu.lh.databindingrecyclerview.databinding.ItemRcvBinding;

import java.util.List;

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private Context context;
    private List<Bean> mList;

    public RecyclerViewAdapter(MainActivity mainActivity, List<Bean> list) {
        context = mainActivity;
        mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ItemRcvBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_rcv, viewGroup, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.getBinding().setBean(mList.get(i));
        viewHolder.getBinding().executePendingBindings();
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ItemRcvBinding binding;

        public ItemRcvBinding getBinding() {
            return binding;
        }

        public ViewHolder(ItemRcvBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

    }
}
